package pers.zyl.juc;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.LinkedBlockingQueue;

public class CollectionsJuc {


    /**
     * ArrayList  YES
     * CopyOnWriteArrayList NO
     * @param list
     * @throws InterruptedException
     */
    public static void ConcurrentModifiedException(List<String> list) throws InterruptedException {
        int threadCount = 10;
        Thread[] t = new Thread[threadCount];
        for (int i = 0; i < threadCount; i++) {
            t[i] = new Thread(() -> {
                list.add(UUID.randomUUID().toString());
                System.out.println(list);
            });
        }
        for (int i = 0; i < threadCount; i++) {
            t[i].start();
        }
        for (int i = 0; i < threadCount; i++) {
            t[i].join();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
        List<Object> objects = Collections.synchronizedList(new ArrayList<>());
        CopyOnWriteArrayList<Object> objects1 = new CopyOnWriteArrayList<>();


        CopyOnWriteArraySet<Object> objects2 = new CopyOnWriteArraySet<>();
        Set<Object> objects3 = Collections.synchronizedSet(new HashSet<>());

        ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>();
        map.put("", "");
        map.put("0", "1");

        LinkedBlockingQueue<String> strings = new LinkedBlockingQueue<>(2);
        strings.add("1");
        strings.add("1");
        strings.put("1");
        strings.take();
//        CollectionsJuc.ConcurrentModifiedException(list);
    }
}
